<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no" />
    <title>自定义指令</title>
    <style>
        [v-cloak] {
            display: none;
        }

        .box {
            width: 100px;
            height: 100px;
            background-color: #0000FF
        }
    </style>
</head>

<body>
    <div id="app" v-cloak></div>
    <script src="js/vue.js"></script>
    <script>
        //全局注册指令
        Vue.directive("focus", {
            // 当被绑定的元素插入到 DOM 中时……
            inserted: (el, binding) => {
                // console.log(el,binding);
                el.focus();
            }
        });
        let App = {
            directives: {
                position: (el, binding) => {
                    // console.log(el, binding);
                    el.style.position = "absolute";
                    el.style[binding.arg] = binding.value + "px";
                }
            },
            template: `
        <div>
            <input type="text" v-focus>
            <div class="box" v-position:left="200" v-position:top="100"></div>
        </div>
      `
        };
        new Vue({
            el: "#app",
            template: `
                <App></App>
        `,
            components: {
                App
            }
        })
    </script>
</body>

</html>